package com.zyb.org.service.impl;

import com.zyb.basic.service.impl.BaseServiceImpl;
import com.zyb.basic.util.QueryResult;
import com.zyb.org.domain.Department;
import com.zyb.org.mapper.DepartmentMapper;
import com.zyb.org.query.DepartmentQuery;
import com.zyb.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Transactional
    @Override
    public void add(Department department) {
        departmentMapper.add(department);
        //处理部门所属路径字段
        String setPath = "";
        if(department.getParent() == null){//判断是否是顶级部门
            setPath =  "/" + department.getId();//拼接自己的id
        }else {
            Department parent = departmentMapper.loadOne(department.getParent().getId());//获取父级部门
            setPath = parent.getDirPath()+"/"+department.getId();//拼接父级部门dir_path+自己的id
        }
        //把拼好的部门所属路径设置进去
        department.setDirPath(setPath);
        departmentMapper.update(department);
    }

    @Transactional
    @Override
    public void update(Department department) {
        //处理部门所属路径字段
        String setPath = "";
        if(department.getParent() == null){//判断是否是顶级部门
            setPath =  "/" + department.getId();//拼接自己的id
        }else {
            Department parent = departmentMapper.loadOne(department.getParent().getId());//获取父级部门
            setPath = parent.getDirPath()+"/"+department.getId();//拼接父级部门dir_path+自己的id
        }
        //把拼好的部门所属路径设置进去
        department.setDirPath(setPath);
        departmentMapper.update(department);
    }

   /* *//*嵌套循环不适用*//*
    @Override
    public List<Department> getTree() {
        //查询所有部门
        List<Department> all = departmentMapper.loadAll();
        //用这个集合装顶级部门
        List<Department> result = new ArrayList<>();
        *//*快捷键iter*//*
        //是顶级部门就放入result中
        for (Department department : all) {
            if(department.getParent_id() == null){
                result.add(department);
            }else {//不是顶级部门，找自己的父部门 ，把子部门放入父部门children集合中
                for (Department parent : all) {
                    if(department.getParent_id() == parent.getId()){//找到父部门
                        List<Department> children = parent.getChildren();//把子部门放入父部门children集合中
                        children.add(department);
                        break;//找到父部门结束内存循环
                    }
                }

            }
        }
        return result;
    }*/

    /*使用map*/
    @Override
    public List<Department> getTree() {

        //查询所有部门
        List<Department> all = departmentMapper.loadAll();
        //把所有部门转成map key是部门id value就是这个部门对象
        // Map<Long, Department> map = loadAll().stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //用这个集合装顶级部门
        List<Department> result = new ArrayList<>();
        /*快捷键iter*/
        //是顶级部门就放入result中
        for (Department department : all) {
            if(department.getParent_id() == null){
                result.add(department);
            }else {//不是顶级部门，找自己的父部门 ，把子部门放入父部门children集合中
                Long pid = department.getParent_id();
                Department parent = map.get(pid);
                if(parent != null){
                List<Department> children = parent.getChildren();
                children.add(department);
                }

            }
        }
        return result;
    }
}
